#!/usr/bin/env python

#Created by David Langelaan 2008
#Filename: Chemshift2excel
#################################
#Version 1.2
#Completed Feb. 24/ 2008
#I added functions for choosing the output directory
#Added a function for selecting which chemical shifts you want to use for calculations. 
#################################
#Version 1.1
#Completed Feb. 16/ 2008
# I added a GUI using wxPython to make it more user friendly. 
##############################
#Version 1.0
#completed Feb. 10, 2008
#############################

#Module assumes that the chemical shift of HB HBa or HB1 is > chemical shift of HBb or HB2 or HB'
'''This module is able to search a sparky peak list for certain
atoms (HA, HB, CA,CB),it then can append user selected random coil shifts and
calculate residual chemical shifts. It prints the data to text files using
search terms +'list' as the filename. The default random coil shifts to use are
the Schwarzinger et al shifts.'''
import os, sys, string, re


  ###The chemical shifts come from Wishart et al!
    ####Note: THe proline shifts used are assumed to be Trans prolines, and the Cyseins are reduced

Resonances = {'A': [['HN', 8.24], ['HA', 4.32], ['HB', 1.39], ['CA', 52.5], ['CB', 19.1]],
            'C': [['HN', 8.32], ['HA', 4.55], ['HB', 2.93], ['CA', 58.2], ['CB', 28.0]],
            'D': [['HN', 8.34], ['HA', 4.64], ['HBa', 2.72], ['HBb', 2.65], ['CA', 54.2], ['CB', 41.1]],
            'E': [['HN', 8.42], ['HA', 4.35], ['HBa', 2.06], ['HBb', 1.96], ['HG', 2.31], ['CA', 56.6], ['CB', 29.9], ['CG', 35.6]],
            'F': [['HN', 8.30], ['HA', 4.62], ['HBa', 3.14], ['HBb', 3.04], ['HD', 7.28], ['HE', 7.38], ['HZ', 7.32], ['CA', 57.7], ['CB', 39.6], ['CD', 131.9], ['CE', 131.5], ['CZ', 129.9]],
            'G': [['HN', 8.33], ['HA', 3.96], ['CA', 45.1]],
            'H': [['HN', 8.42], ['HA', 4.73], ['HBa', 3.29], ['HBb', 3.16], ['HD2', 8.58], ['HE1',7.29], ['HE2', 0.00], ['CA', 55.0], ['CB', 29.0], ['CD2', 136.2], ['CE1', 131.1]],
            'I': [['HN', 8.00], ['HA', 4.17], ['HB', 1.87], ['HG11', 1.45], ['HG12', 1.16], ['HG2', 0.91], ['HD', 0.86], ['CA', 61.1], ['CB', 38.8], ['CG1', 27.2], ['CG2', 17.4], ['CD', 12.9]], 
            'K': [['HN', 8.29], ['HA', 4.32], ['HBa', 1.84], ['HBb', 1.75], ['HG', 1.44], ['HD', 1.68], ['HE', 2.99], ['HZ', 7.81], ['CA', 56.2], ['CB', 33.1], ['CG', 24.7], ['CD', 29.0], ['CE', 41.9]],
            'L': [['HN', 8.16], ['HA', 4.34], ['HB', 1.62], ['HG', 1.59], ['HD1', 0.92], ['HD2', 0.87], ['CA', 55.1], ['CB', 42.4], ['CG', 26.9], ['CD1', 24.9], ['CD2', 23.3]],
            'M': [['HN', 8.28], ['HA', 4.48], ['HBa', 2.11], ['HBb', 2.01], ['HGa',2.60], ['HGb', 2.54], ['HE', 2.10], ['CA', 55.4], ['CB', 32.9], ['CG', 32.0], ['CE', 16.9]],
            'N': [['HN', 8.40], ['HA', 4.74], ['HBa', 2.83], ['HBb', 2.75], ['HD2a', 7.59], ['HD2b', 6.91], ['CA', 53.1], ['CB', 38.9]],
            'P': [['HA', 4.42], ['HBa', 2.29], ['HBb', 1.94], ['HG', 2.02], ['HD', 3.63], ['CA', 63.3], ['CB', 32.1], ['CG', 27.2], ['CD', 49.8]],
            'Q': [['HN', 8.32], ['HA', 4.34], ['HBa', 2.12], ['HBb', 1.99], ['HG', 2.36], ['HE2a', 7.52], ['HE2b', 6.85], ['CA', 55.7], ['CB', 29.4], ['CG', 33.7]],
            'R': [['HN', 8.23], ['HA', 4.34], ['HBa', 1.86], ['HBb', 1.76], ['HG', 1.63], ['HD', 3.20], ['HE', 8.07], ['HH', 0.00], ['CA', 56.0], ['CB', 30.9], ['CG', 27.1], ['CD', 43.3]],
            'S': [['HN', 8.31], ['HA', 4.47], ['HBa', 3.89], ['HBb', 3.87], ['CA', 58.3], ['CB', 63.8]],
            'T': [['HN', 8.15], ['HA', 4.35], ['HB', 4.24], ['HG', 1.21], ['CA', 61.8], ['CB', 69.8], ['CG', 21.5]],
            'V': [['HN', 8.03], ['HA', 4.12], ['HB', 2.08], ['HG1', 0.94], ['HG2', 0.93], ['CA', 62.2], ['CB', 32.9], ['CG1', 21.1], ['CG2', 20.3]],
            'W': [['HN', 8.25], ['HA', 4.66], ['HBa', 3.29], ['HBb', 3.27],['HD1', 7.27], ['HE3', 7.65], ['HZ3', 7.18], ['HH2', 7.25], ['HZ2', 7.50], ['CA', 57.5], ['CB', 29.6], ['CD1', 127.4], ['CE3', 122.2], ['CE2', 124.8], ['CZ3', 121.0], ['CZ2', 114.7]],
            'Y': [['HN', 8.12], ['HA', 4.55], ['HBa', 3.03], ['HBb', 2.98], ['HD', 7.14], ['HE', 6.84],['HH', 0.00], ['CA', 57.9], ['CB', 38.8], ['CD', 133.3], ['CE', 118.2]]}
						

searchlist = ['HA','CA','HB','CB']
inputfile = '/Users/Dave/Desktop/shifts'#inputfile is expected to be a sparky resonancelist. The same naming conventions as up in the list above must be followed in the resonance list.
sequence = 'KFRRQRPRLSHKGPMPF'



f = open(inputfile)
text = f.readlines()
f.close()
formtext = []
for res in text:
	residue = string.split(res)
	formtext.append(residue)
title = formtext[0]
title.append('Randomshift')
title.append('Obs-Random')
print '%-10s%-10s%-10s%-10s%-10s%-15s%-15s%-15s' %(title[0],title[1],title[2],title[3],title[4],title[5],title[6],title[7])
for atom in searchlist:
	for index, resi in enumerate(sequence):
		for line in formtext[2:]:
			if line[0] == resi+str(index+1) and line[1][0:2] ==atom:
				for reson in Resonances[resi]:
					if reson[0] == line[1]:
						print '%-10s%-10s%-10s%-10s%-10s%-15s%-15s%6.3f' %(line[0],line[1],line[2],line[3],line[4],line[5], reson[1], float(line[3]) -reson[1])
					elif reson[0] == line[1][0:2]:
						print '%-10s%-10s%-10s%-10s%-10s%-15s%-15s%6.3f' %(line[0],line[1],line[2],line[3],line[4],line[5], reson[1], float(line[3]) -reson[1])
					elif reson[0][0:2] == line[1]:
						print '%-10s%-10s%-10s%-10s%-10s%-15s%-15s%6.3f' %(line[0],line[1],line[2],line[3],line[4],line[5], reson[1], float(line[3]) -reson[1])

#to add/do:
#check it over with a previous data set
#check to see if my calculation of residual chemical shift is correct
#add a text on the main window, to let user know when script finishes or about errors. 
#Set unix computer so it works better...
#proofread the dictionary random coil shifts. 
#transform the script to a more object oriented approach/rewrite the program more efficiently....

#automatically load it into excel or graph results using some other program?..
#add in support for other hydrogens and atoms(HG,HD)

#to put in program rewrite. 
#Use 'F' in 'Fairy' to search strings or lists instead of re.
#use remove instead of del statement, x.remove('')
#Is it really necessary to convert the lists to strings and then back?
# use % for formatting strings/ file output
#have lists of dictionaries for the chemical shifts


